var Page = function(){
    this.init();
};
Page.prototype = {
    params: {
        passport: '',
        page:1,
        listRows:10,
        total: 0,
        name:'',
        position:'',
        endTime:'',
        idArrUnique:[],
        nochecked:[]
    },
    init: function(){
        this.initParams();
        this.initEvent();
    },
    initParams: function(){
        this.params.passport = Pub.getCookie('_PASSPORT');
    },
    getData: function(){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            page:_this.params.page,
            listRows:_this.params.listRows,
            name:_this.params.name,
            position:_this.params.position,
            endTime:_this.params.endTime
        };
        console.log(paramsObj);
        Api.adlist(paramsObj, function(data){
            console.log(data);
            _this.getDataList(data.list);
            _this.params.total = data.totalPage;
            layui.use('laypage', function(){
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                  elem: 'page',
                  index: 1,
                  theme: '#3498db',
                  curr:  _this.params.page,
                  count: _this.params.total, //数据总数，从服务端得到
                  hash: 'curr_page',
                  jump: function(obj, first){
                    _this.params.page = obj.curr;
                    //obj包含了当前分页的所有参数，比如：curr当前页
                    var Params = {
                        passport: _this.params.passport,
                        page:_this.params.page,
                        listRows:_this.params.listRows,
                        name:_this.params.name,
                        position:_this.params.position,
                        endTime:_this.params.endTime
                    };
                    Api.adlist(Params, function(data){
                        _this.getDataList(data.list);
                        $('input[type=checkbox][name=new]').each(function() {
                            var self=$(this);
                            _this.params.idArrUnique.forEach(function(item,index){
                                if (self.attr('adId')==item) {
                                    self.iCheck('check');
                                }
                            });
                        });
                        if ($('input[type=checkbox][name=new]:checked').length==$('#List tr').length) {
                             $('input[type=checkbox][name=all]').parent().addClass('checked');
                        }else{
                            $('input[type=checkbox][name=all]').parent().removeClass('checked');
                        };
                    })
                  }
                });
            });
        });
    },
    getDataList: function (data) {
        var _this = this;
        if (data && data.length > 0) {
            var html = '', i = 0, len = data.length;
            for (i; i < len; i++) {
                html+='<tr>'+
                        '<td>'+
                            '<div class="goods-checkbox" >'+
                                '<div class="check-box" adId="'+data[i].adId+'">'+
                                    '<input type="checkbox" name="new" adId="'+data[i].adId+'">'+
                                '</div>'+
                            '</div>'+
                        '</td>'+
                        '<td class="text-c">'+data[i].adId+'</td>'+
                        '<td class="text-c">'+data[i].name+'</td>';
                if (data[i].position=='index') {
                    html+='<td class="text-c">'+'首页'+'</td>';
                }else if(data[i].position=='project'){
                    html+='<td class="text-c">'+'资源项目'+'</td>';
                }else if(data[i].position=='recommend'){
                    html+='<td class="text-c">'+'推荐方案'+'</td>';
                }else if(data[i].position=='mailing'){
                    html+='<td class="text-c">'+'方案投递'+'</td>';
                };


                html+=
                        '<td class="text-c">'+
                            '<p>开始时间：'+data[i].startTime+'</p>'+
                            '<p>结束时间：'+data[i].endTime+'</p>'+
                        '</td>'+
                        '<td class="text-c">';
                if (data[i].isOnline==1) {
                    html+='<span adId="'+data[i].adId+'" class="switch switch-on new" themeColor="#3498db"></span>';
                }else{
                    html+='<span adId="'+data[i].adId+'" class="switch switch-off new" themeColor="#3498db"></span>';
                };

                html+='</td>'+
                        '<td class="text-c">'+
                            '<a class="handle edit" adId="'+data[i].adId+'">编辑</a>';
                html+=  '<a class="handle delete" adId="'+data[i].adId+'">删除</a>'+
                        '</td>'+
                    '</tr>';
                html+=
                        '</td>'+
                    '</tr>';
            }
            $('#List').html(html);

            $('tfoot').css('display', 'table-footer-group');
            _this.getDataEvent();
        }else{
            var html='<tr>'+
                        '<td class="text-c c-666" colspan="7" >暂无数据</td>'+
                    '</tr>';
            $('#List').html(html);
            $('tfoot').css('display', 'none');
        }
    },
    getDataEvent:function(){
        var _this = this;
        honeySwitch.init();
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('.edit').on('click', function() {
            Pub.setSession('adId', $(this).attr('adId'));
            Pub.setSession('adsEdit', 1);
            window.location.href = Pub.getHtmlUrl('./ads-list-edit.html');
        });
        $('.delete').click(function(event) {
            var id=$(this).attr('adId');
            var _self=this;
            layui.use('layer', function(){
                  layer.confirm('您确定要删除吗？', {
                    btn: ['确定','取消'] //按钮
                  }, function(index){
                        _this.deleteAd(id);
                        layer.close(index);
                  }, function(){

                  });
            });
        });
        $('input[type=checkbox][name=all]').on('ifChanged', function() {
            if ($(this).parent().hasClass('checked')) {
                $('input[type=checkbox][name=new]').iCheck('uncheck');
                setTimeout(function(){
                    $('input[type=checkbox][name=all]').parent().addClass('unchecked');
                },100)
            } else {
                $('input[type=checkbox][name=new]').iCheck('check');
                setTimeout(function(){
                    $('input[type=checkbox][name=all]').parent().addClass('checked');
                },100)
            }
        });
        setTimeout(function(){
             $('input[type=checkbox][name=new]').on('ifChanged', function() {
                //获取选取的并加入数组
                _this.params.idArrUnique.push($(this).attr('adId'));

                //数组去重
                _this.params.idArrUnique=_this.params.idArrUnique.unique();

                // console.log(_this.params.idArrUnique);

                //获取未选取的并加入数组
                for (var i = 0; i < $('input[type=checkbox][name=new]').length; i++) {
                     if ($('input[type=checkbox][name=new]').eq(i).prop('checked')) {
                        _this.params.nochecked.remove($('input[type=checkbox][name=new]').eq(i).attr('adId'));
                         _this.params.nochecked=_this.params.nochecked.unique();
                     }else{
                        _this.params.nochecked.push($('input[type=checkbox][name=new]').eq(i).attr('adId'));
                        _this.params.nochecked=_this.params.nochecked.unique();
                     };
                };
                //去除x选取数组中未选取的ID
                for (var i = 0; i < _this.params.nochecked.length; i++) {
                    // console.log(nochecked[i]);
                    _this.params.idArrUnique.remove(_this.params.nochecked[i]);
                };
                if ($('input[type=checkbox][name=new]:checked').length==$('#List tr').length) {
                     $('input[type=checkbox][name=all]').parent().addClass('checked');
                }else{
                    $('input[type=checkbox][name=all]').parent().removeClass('checked');
                };
                //console.log(_this.params.idArrUnique);
            });
        },500)
        $('.switch').on('click',function(event) {
            event.preventDefault();
            var adId=$(this).attr('adId');
            var datas=null;
            if ($(this).hasClass('switch-on')) {
                datas=1;
            }else{
                datas=0;
            };
            _this.changeOnline(adId,datas);
        });

    },
    deleteAd:function(id){
        var _this = this;
        console.log(id);
        var paramsObj = {
            passport: _this.params.passport,
            adIds:id
        };
        console.log(paramsObj);
        Api.addel(paramsObj, function(data){
            console.log(data);
            layui.use('layer', function(){
                layer.msg('删除成功', {icon: 6});
            });
            _this.params.page=1;
            _this.getData();
        });
    },
    deleteAll:function(){
        var _this = this;
        var adIds='';
         if (_this.params.idArrUnique.length>0) {

            adIds=_this.params.idArrUnique.join(",");
        }
        console.log(adIds);
        if (_this.params.idArrUnique.length>0) {
            var Params = {
                passport: _this.params.passport,
                adIds: adIds
            };
            Api.addel(Params, function(data){
                layui.use('layer', function(){
                    layer.msg('删除成功', {icon: 6});
                });
                _this.params.page=1;
                _this.getData();
            });
        }else{
            layui.use('layer', function(){
                layer.msg('请先选择要删除的图片', {icon: 5});
            });
        };
    },
    changeOnline:function(adId,isOnline){
        var _this = this;
        var paramsObj = {
            passport: _this.params.passport,
            adId:adId,
            isOnline:isOnline,
        };
        Api.adoperate(paramsObj, function(data){
            console.log(data);
        });
    },
    search:function(){
        var _this = this;
        $('#normalSearch').click(function(event) {
            _this.params.name=$('#name').val();

            _this.params.page=1;
            _this.getData();
        });
    },
    otherEvent:function(){
        var _this = this;
        $('.goods-checkbox input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        })
        $('.slectShow').click(function(event) {
            $('.select-wrap').toggle(400);
        });
        $('#deAll').click(function(event) {
            if ($('#allTypeSelect').val()==1) {
                layui.use('layer', function(){
                      layer.confirm('您确定要删除所选图片吗？', {
                        btn: ['确定','取消'] //按钮
                      }, function(index){
                            _this.deleteAll();
                            layer.close(index);
                      }, function(){

                      });
                });
            };

        });
        $('#changeSize').on('change', function(event) {
            event.preventDefault();

            _this.params.listRows=$(this).val();
            $('#page').html('');
            _this.params.page=1;
            _this.getData();

        });
        $('#changeTime').on('change', function(event) {
            event.preventDefault();
            _this.params.endTime = $(this).val();
        });
        $('#changeDesc').on('change', function(event) {
            event.preventDefault();
            if ($(this).val() !== '排序方式') {
                _this.params.order = $(this).val();
                $('#page').html('');
                _this.params.page=1;
                _this.getData();
            };
        });
        $('#typeSelect').on('change', function(event) {
            event.preventDefault();
            _this.params.position=$(this).val();
        });
        $('#add_pic').on('click', function() {
            Pub.setSession('adsEdit', 0);
            window.location.href = Pub.getHtmlUrl('./ads-list-edit.html');
        });
    },
    initEvent: function(){
        var _this = this;
        _this.getData();
        _this.otherEvent();
        _this.search();

    }
};
var page = new Page();